package com.bingkaimedia.dompetobeta.libs;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.bingkaimedia.dompetobeta.app.Signin;

public class SessionManager {
	// Shared Preferences
	SharedPreferences pref;
	
	// Editor for Shared preferences
	Editor editor;
	
	// Context
	Context _context;
	
	// Shared pref mode
	int PRIVATE_MODE = 0;
	
	// Sharedpref file name
	private static final String PREF_NAME = "Diskon_Banget";
	
	// All Shared Preferences Keys
	private static final String IS_LOGIN = "IsLoggedIn";
	  
	public static final String id_users = "id_users_";
	public static final String username = "username_";
	public static final String password = "password_";
	public static final String email = "email_";
	public static final String country = "country_"; 
	public static final String country_name = "country_name_"; 
	public static final String city = "city_"; 
	public static final String city_name = "city_name_"; 
	public static final String checked = "checked_"; 
	public static final String channel = "channel_"; 
	public static final String category = "category_"; 
	public static final String category_name = "category_name_"; 
	
	// Constructor
	public SessionManager(Context context){
		this._context = context;
		pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
		editor = pref.edit();
	}
	
	/**
	 * Create login session
	 * */
	public void createLoginSession(String id_users_, String username_, String password_ ,String email_, String country_, String country_name_,String city_, String city_name_, String checked_, String channel_, String category_,String category_name_){
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);
		  
		editor.putString(id_users, id_users_);
		editor.putString(username, username_);
		editor.putString(password, password_);
		editor.putString(email, email_);
		editor.putString(country, country_);
		editor.putString(country_name, country_name_);
		editor.putString(city, city_);
		editor.putString(city_name, city_name_);
		editor.putString(checked, checked_); 
		editor.putString(channel, channel_); 
		editor.putString(category, category_); 
		editor.putString(category_name, category_name_); 
		
		// commit changes
		editor.commit();
		
		/*
		System.out.println("session manager");
		System.out.println(id_users_);
		System.out.println(username_);
		System.out.println(password_);
		System.out.println(email_);
		System.out.println(country_);
		System.out.println(country_name_);
		System.out.println(city_);
		System.out.println(city_name_);
		System.out.println(checked_);
		System.out.println(channel_);
		System.out.println(category_);
		System.out.println(category_name_);
		*/
	}	
	
	/**
	 * Check login method wil check user login status
	 * If false it will redirect user to login page
	 * Else won't do anything
	 * */
	public void checkLogin(){
		// Check login status
		if(!this.isLoggedIn()){
			// user is not logged in redirect him to Login Activity
			Intent i = new Intent(_context, Signin.class);
			// Closing all the Activities
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			// Add new Flag to start new Activity
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			
			// Staring Login Activity
			_context.startActivity(i);
		}
		
	}
	
	
	
	
	/**
	 * Get stored session data
	 * */
	public HashMap<String, String> getUserDetails(){
		HashMap<String, String> user = new HashMap<String, String>();
		// user name
		
		 
		user.put(id_users, pref.getString(id_users, null));
		user.put(username, pref.getString(username, null));
		user.put(password, pref.getString(password, null));
		user.put(email, pref.getString(email, null));
		user.put(country, pref.getString(country, null));
		user.put(country_name, pref.getString(country_name, null));
		user.put(city, pref.getString(city, null));
		user.put(city_name, pref.getString(city_name, null));
		user.put(checked, pref.getString(checked, null));
		user.put(channel, pref.getString(channel, null));
		user.put(category, pref.getString(category, null));
		user.put(category_name, pref.getString(category_name, null));
		
 
		
		// return user
		return user;
	}
	
	/**
	 * Clear session details
	 * */
	public void logoutUser(){
		// Clearing all data from Shared Preferences
		editor.clear();
		editor.commit();
		
		// After logout redirect user to Loing Activity
		Intent i = new Intent(_context, Signin.class);
		// Closing all the Activities
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
		// Add new Flag to start new Activity
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		// Staring Login Activity
		_context.startActivity(i);
	}
	
	/**
	 * Quick check for login
	 * **/
	// Get Login State
	public boolean isLoggedIn(){
		return pref.getBoolean(IS_LOGIN, false);
	}
	
	public String nama()
	{
		return pref.getString(username, "kosong");
	}
}
